package org.feifei.org.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.feifei.basic.exception.BusinessException;
import org.feifei.basic.util.CommonResult;
import org.feifei.basic.util.PageList;
import org.feifei.org.domain.Shop;
import org.feifei.org.query.ShopQuery;
import org.feifei.org.service.ShopService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * Shop接口
 */
@RestController
@Api(tags = "ShopController", description = "员工接口")
@RequestMapping("/shop")
//@CrossOrigin("http://127.0.0.1:8080")
public class ShopController {

    @Resource
    private ShopService shopService;

    /**
     * 不带条件查询所有，展示所有数据
     * @return
     */
    @GetMapping("/all")
    @ApiOperation(value = "不带条件查询所有", notes = "直接查看该表下的所有数据")
    public CommonResult list() {
        try {
            List<Shop> list = shopService.list();
            return CommonResult.ok(list);
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failure(e.getMessage());
        }
    }

    /**
     * 通过id获取单个实体类的数据
     * @param id Shop类的id
     * @return
     */
    @ApiOperation(value = "通过id获取单个实体类的数据", notes = "根据id,来获取Shop中,是输入值id的员工 ")
    @GetMapping("/{id}")
    public CommonResult getById(@PathVariable("id")Long id) {
        try {
            Shop shop = shopService.get(id);
            return CommonResult.ok(shop);
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failure(e.getMessage());
        }
    }

    /**
     * 根据id删除 店铺
     * @param id Shop类的id
     * @return
     */
    @ApiOperation(value = "根据id删除员工信息", notes = "通过传入的员工id,先删除该员工的相关信息，再删除员工信息")
    @DeleteMapping("/{id}")
    public CommonResult del(@PathVariable("id")Long id) {
        try {
            shopService.remove(id);
            return CommonResult.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failure(e.getMessage());
        }
    }

    /**
     * 添加 新的店铺
     * @param shop 商店的实体类
     * @return
     */
    @ApiOperation(value = "添加新的员工",notes = "填入新的员工信息，来创建一个新的员工")
    @PostMapping
    public CommonResult add(@RequestBody Shop shop) {
        try {
            shopService.save(shop);
            return CommonResult.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failure(e.getMessage());
        }
    }

    /**
     * 修改 该商店的某些属性
     * @param shop 实体类 Shop
     * @return
     */
    @ApiOperation(value = "修改部分的信息", notes = "根据传入的员工实体类，需要携带员工id，来修改该员工的信息")
    @PutMapping
    @Deprecated
    public CommonResult update(@RequestBody Shop shop) { // @RequestBody 从请求体中获取参数
        try {
            shopService.update(shop);
            return CommonResult.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failure(e.getMessage());
        }
    }

    /**
     * 高级查询，分页查询，条件查询
     * @param query 高级查询类 ShopQuery
     * @return
     */
    @ApiOperation(value = "根据查询条件", notes = "包含了分页查询，高级查询")
    @PostMapping("/list")
    public PageList<List<Shop>> listByPage(@RequestBody ShopQuery query) {
        return shopService.listByPage(query);
    }

    /**
     * 批量删除，根据传过来的id的集合，批量删除
     *
     * @param ids id集合
     * @return
     */
    @ApiOperation(value = "批量删除", notes = "根据传过来的删除数据")
    @PatchMapping
    public CommonResult bacthRemove(@RequestBody List<Long> ids) {
        try {
        shopService.batchRemove(ids);
        return CommonResult.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failure();
        }
    }

    @PostMapping("/settlement")
    @ApiOperation("店铺入住注册")
    public CommonResult settlement(@RequestBody Shop shop) {
        try {
            shopService.settlement(shop);
            return CommonResult.ok();
        } catch (BusinessException e) {
            e.printStackTrace();
            return CommonResult.failure();
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failure();
        }
    }
}
